package Views.TableModels;

import Models.Route;
import java.util.ArrayList;

public class RouteTableModel extends TableModelAbstract<Route>
{
	public RouteTableModel()
	{
		this.data = new ArrayList<Route>();
	}

	public RouteTableModel(ArrayList<Route> routes)
	{
		this.data = routes;
	}

	@Override
	public boolean isCellEditable(int row, int column)
	{
		return false;
	}

	@Override
	public int getColumnCount()
	{
		return 4;
	}

	@Override
	public int getRowCount()
	{
		if(this.data == null)
		{
			return 0;
		}
		else
		{
			return this.data.size();
		}
	}

	@Override
	public String getColumnName(int column)
	{
		String columnName = "";
		switch(column)
		{
			case 0:
				columnName = "RuteNr";
				break;
			case 1:
				columnName = "Længde";
				break;
			case 2:
				columnName = "Tid";
				break;
			case 3:
				columnName = "Kunder";
				break;
		}
		return columnName;
	}

	@Override
	public Object getValueAt(int row, int column)
	{
		Object value = null;
		switch(column)
		{
			case 0:
				value = this.data.get(row).getRouteNo();
				break;
			case 1:
				value = this.data.get(row).getTotalDistance();
				break;
			case 2:
				value = this.data.get(row).getTotalTimeToString();
				break;
			case 3:
				value = getCustomerValue(row);
				break;
		}
		return value;
	}

	private String getCustomerValue(int row)
	{
		String customers = "";
		int i = 0;
		int length;

		if(this.data.get(row).getLocations().size() >= 5)
		{
			length = 5;
		}
		else
		{
			length = this.data.get(row).getLocations().size();
		}

		while(i < length)
		{
			customers += this.data.get(row).getLocations().get(i).getCustomerName();
			if(i != 4) customers += ", ";
			i++;
		}
		
		return customers;
	}
}
